package vampirism.core.client;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class ExampleInterface implements ApplicationListener {
        String[] listEntries = {"This is a list entry", "And another one", "The meaning of life", "Is hard to come by",
                "This is a list entry", "And another one", "The meaning of life", "Is hard to come by", "This is a list entry",
                "And another one", "The meaning of life", "Is hard to come by", "This is a list entry", "And another one",
                "The meaning of life", "Is hard to come by", "This is a list entry", "And another one", "The meaning of life",
                "Is hard to come by"};

        Skin skin;
        Stage stage;
        SpriteBatch batch;
        Texture texture1;
        Texture texture2;
        Label fpsLabel;

        @Override
        public void create () {
                batch = new SpriteBatch();
                Texture.setEnforcePotImages(false); 
                skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
                texture1 = new Texture(Gdx.files.internal("skin/badlogicsmall.jpg"));
                texture2 = new Texture(Gdx.files.internal("skin/badlogic.jpg"));
                TextureRegion image = new TextureRegion(texture1);
                TextureRegion imageFlipped = new TextureRegion(image);
                imageFlipped.flip(true, true);
                TextureRegion image2 = new TextureRegion(texture2);
                stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
                Gdx.input.setInputProcessor(stage);

                // Group.debug = true;

                ImageButtonStyle style = new ImageButtonStyle(skin.get(ButtonStyle.class));
                style.imageUp = new TextureRegionDrawable(image);
                style.imageDown = new TextureRegionDrawable(imageFlipped);
                ImageButton iconButton = new ImageButton(style);

                Button buttonMulti = new TextButton("Multi\nLine\nToggle", skin, "toggle");
                Button imgButton = new Button(new Image(image), skin);
                Button imgToggleButton = new Button(new Image(image), skin, "toggle");

                CheckBox checkBox = new CheckBox("Check me", skin);
                final Slider slider = new Slider(0, 10, 1, false, skin);
                TextField textfield = new TextField("", skin);
                textfield.setMessageText("Click here!");
                SelectBox dropdown = new SelectBox(new String[] {"Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX","Android", "Windows", "Linux", "OSX"}, skin);
                Image imageActor = new Image(image2);
                ScrollPane scrollPane = new ScrollPane(imageActor);
                List list = new List(listEntries, skin);
                ScrollPane scrollPane2 = new ScrollPane(list, skin);
                scrollPane2.setFlickScroll(false);
                SplitPane splitPane = new SplitPane(scrollPane, scrollPane2, false, skin, "default-horizontal");
                fpsLabel = new Label("fps:", skin);

                // configures an example of a TextField in password mode.
                final Label passwordLabel = new Label("Textfield in password mode: ", skin);
                final TextField passwordTextField = new TextField("", skin);
                passwordTextField.setMessageText("password");
                passwordTextField.setPasswordCharacter('*');
                passwordTextField.setPasswordMode(true);

                // window.debug();
                Window window = new Window("Dialog", skin);
                //window.getButtonTable().add(new TextButton("X", skin)).height(window.getPadTop());
                window.setPosition(0, 0);
                window.defaults().spaceBottom(10);
                window.row().fill().expandX();
                window.add(iconButton);
                window.add(buttonMulti);
                window.add(imgButton);
                window.add(imgToggleButton);
                window.row();
                window.add(checkBox);
                window.add(slider).minWidth(100).fillX().colspan(3);
                window.row();
                window.add(dropdown);
                window.add(textfield).minWidth(100).expandX().fillX().colspan(3);
                window.row();
                window.add(splitPane).fill().expand().colspan(4).maxHeight(200);
                window.row();
                window.add(passwordLabel).colspan(2);
                window.add(passwordTextField).minWidth(100).expandX().fillX().colspan(2);
                window.row();
                window.add(fpsLabel).colspan(4);
                window.pack();

                // stage.addActor(new Button("Behind Window", skin));
                stage.addActor(window);

                textfield.setTextFieldListener(new TextFieldListener() {
                        public void keyTyped (TextField textField, char key) {
                                if (key == '\n') textField.getOnscreenKeyboard().show(false);
                        }
                });

                slider.addListener(new ChangeListener() {
                        public void changed (ChangeEvent event, Actor actor) {
                                Gdx.app.log("UITest", "slider: " + slider.getValue());
                        }
                });

                iconButton.addListener(new ChangeListener() {
                        public void changed (ChangeEvent event, Actor actor) {
                                new Dialog("Some Dialog", skin, "dialog") {
                                        protected void result (Object object) {
                                                System.out.println("Chosen: " + object);
                                        }
                                }.text("Are you enjoying this demo?").button("Yes", true).button("No", false).key(Keys.ENTER, true)
                                        .key(Keys.ESCAPE, false).show(stage);
                        }
                });
        }

        @Override
        public void render () {
                Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
                Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

                fpsLabel.setText("fps: " + Gdx.graphics.getFramesPerSecond());

                stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
                stage.draw();
                Window.drawDebug(stage);
        }

        @Override
        public void resize (int width, int height) {
                stage.setViewport(width, height, false);
        }

        @Override
        public void dispose () {
                stage.dispose();
                skin.dispose();
                texture1.dispose();
                texture2.dispose();
        }

		@Override
		public void pause() {
		}

		@Override
		public void resume() {
		}
}
